package com.struct.graph.create_role
{
	import com.collection.ACollection;
	import com.collection.ActionCollection;
	import com.component.ComponentChildren;
	import com.events.CreateRoleSelectEvent;
	import com.utils.GDI;
	import com.utils.Utils;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.events.EventDispatcher;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	
	public class CreateRoleShow extends ComponentChildren
	{
		private var txtName:TextField;
		private var dispatcher:EventDispatcher;
		public var roleSource:BitmapData;
		public function CreateRoleShow(name:String, roleSource:BitmapData, dispatcher:EventDispatcher)
		{
			super();
			this.dispatcher = dispatcher;
			this.roleSource = roleSource;
			buttonMode = true;
			width = 64;
			height = 84;
			txtName = Utils.createTextField(this, 0, 0, name, 12, 0x0, true, TextFieldAutoSize.CENTER, width);
			
			
			var temp:BitmapData = new BitmapData(64, 64);
			var rsx:uint = Math.floor(Math.random() * 4) * 64;
			var rsy:uint = Math.floor(Math.random() * 8) * 64;
			temp.copyPixels(roleSource, new Rectangle(rsx, rsy, 64, 64), new Point());
			
			var bitMap:Bitmap = new Bitmap(temp);
			bitMap.y = 20;
			addChild(bitMap);
			
			this.addEventListener(MouseEvent.CLICK, clickHandler);
		}
		
		private function clickHandler(e:MouseEvent):void{
			selected = true;
			dispatcher.dispatchEvent(new CreateRoleSelectEvent(ACollection.SELECTED, this));
		}
		public function set selected(value:Boolean):void{
			graphics.clear();
			if(value){
				graphics.beginFill(0xff0000);
				graphics.drawRect(0, 0, width, height);
				graphics.drawRect(4, 4, width - 8, height - 8);
				graphics.endFill();
			}
		}
	}
}